package com.software.shipper.servlet;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.software.common.CommonUtils;
import com.software.shipperagent.dao.ShipperAgent;
import com.software.shipperagent.dao.ShipperAgentTransaction;

public class QuerySagentsServlet extends HttpServlet {

	@Override
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		ShipperAgentTransaction sat = (ShipperAgentTransaction)CommonUtils.getBean("shipperAgentTransaction");
		List<ShipperAgent> agents = sat.getAllAgents();
		List list = new ArrayList();
		if(agents != null){
			for(int i= 0 ; i < agents.size(); i++){
				JSONArray array = new JSONArray();
				array.put(agents.get(i).getName());
				array.put(agents.get(i).getEmail());
				array.put(agents.get(i).getDescription());
				list.add(array);
			}
			JSONObject json = new JSONObject();
			try {
				json.put("agents", list);
				CommonUtils.outputJsonData(json.toString(), response);
			} catch (JSONException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		super.doPost(req, resp);
	}

	
}
